/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Sessions;

import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import models.Account;

/**
 *
 * @author samus
 */
@Stateful
public class AuthentificationBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    private Account user;
    @PersistenceContext
    private EntityManager em;
        
    private String login;
    private String password;
    
    private String errormsg ="error";
    
    public Account getUser() {
        return user;
    }

    public void setUser(Account user) {
        this.user = user;
    }
     public boolean isAuthenticated(){
         return user!=null;
     }
    
    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    public void testLogin() {
           System.out.println("test login en cours");
        String s = "select a from Account a where login=:a and password=:b"; //jpql
        Query query = em.createQuery(s).setParameter("a", this.login).setParameter("b", this.password);
        this.user = (Account) query.getSingleResult();
        System.out.println("test login terminé");
    }

    public String getErrormsg() {
        return errormsg;
    }

    public void setErrormsg(String errormsg) {
        this.errormsg = errormsg;
    }
    
    
}
